package com.yd.mvc.mobile.userController;

import com.yd.app.service.mobile.userService.MUserService;
import com.yd.app.service.pc.roleService.RoleService;
import com.yd.app.util.BusinessException;
import com.yd.domain.commom.PageModel;
import com.yd.domain.sys.Perm;
import com.yd.domain.sys.Role;
import com.yd.domain.sys.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;


@RestController
public class MUserController {

	@Autowired
	MUserService userService;
	@Autowired
	RoleService roleService;

	/**
	 * 用户信息注册
	 * @param user
	 * @return
	 * @throws BusinessException
     */
    @RequestMapping(value = "/dapi/muser/register", method = RequestMethod.POST)
    public String userRegister(@RequestBody User user) throws BusinessException {
        String registCode = "";
        try{
			if (user != null) {
				registCode = userService.userRegister(user);
			}
		}catch (BusinessException e){
			throw e;
		}

        return registCode;
    }

    /**
     * 修改用户信息
     *
     * @param user
     * @throws Exception
	@RequestMapping(value = "/dapi/muser/register", method = RequestMethod.POST)
	public String userRegister(@RequestBody User user)throws Exception {
		String registCode="";
		if(user!=null){
			registCode=userService.userRegister(user);
		}
		return registCode;
	}

	/**
	 * @Author: zy
	 * @Desc: 根据用户名注册码查询用户信息，是否通过审批等信息
	 * @Date:  2018/3/23
	 * @Param
	 */
	@RequestMapping(value = "/dapi/muser/getUserByReg", method = RequestMethod.POST)
	public User getUserByReg(@RequestBody User user)throws Exception{
		User u=new User();
		if(user!=null) {
			u = userService.getUserByReg(user);
		}
		if(u==null){
			u=new User();
			u.setCheckStatus("3");//没有查询到数据
		}
		return u;
	}
	/**
	 * 修改用户信息
	 * @param user
	 * @throws Exception
     */
    @RequestMapping(value = "/dapi/muser/registerUpdate", method = RequestMethod.POST)
    public void userUpdate(@RequestBody User user) throws Exception {
        if (user != null) {
            userService.userUpdate(user);
        }
    }

    /**
     * 通过用户id查询用户信息
     *
     * @param userId
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/api/muser/detail/{userId}", method = RequestMethod.GET)
    public User userDetail(@PathVariable("userId") String userId) throws Exception {
        User user = userService.getUserById(userId);
        return user;
    }
	/**
	 * 获取登录用户信息
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/mapi/muser/detail", method = RequestMethod.GET)
	public User loginUserDetail(HttpSession session) throws Exception {
		User u = (User) session.getAttribute("login_user");
		//数据获取成功---更新已读状态
		if(u!=null){
			return u;
		}else{
			throw new BusinessException(401,"尚未登录","no-login");
		}
	}
	//修改密码
	@RequestMapping(value = "/mapi/muser/updatePwd/{oldPwd}/{newPwd}", method = RequestMethod.GET)
	public void updateUserPwd(@PathVariable("oldPwd")String oldPwd,@PathVariable("newPwd")String newPwd, HttpSession session) throws Exception {
		User u = (User) session.getAttribute("login_user");
		//数据获取成功---更新已读状态
		if(u!=null){
			userService.updateUserPwd(u.getId(),oldPwd,newPwd);
		}else{
			throw new BusinessException(401,"尚未登录","no-login");
		}
	}
    /**
     * 通过用户id查询用户角色信息
     *
     * @param userId
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/api/muser/role/{userId}", method = RequestMethod.GET)
    public List<Role> getUserRole(@PathVariable("userId") String userId) throws Exception {
        List<Role> list = roleService.getUserRole(userId);
        return list;
    }

    /**
     * 通过用户id查询用户权限信息
     *
     * @param userId
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/api/muser/perm/{userId}", method = RequestMethod.GET)
    public List<Perm> getUserPerm(@PathVariable("userId") String userId) throws Exception {
        List<Perm> list = roleService.getUserPerm(userId);
        return list;
    }

}
